<template>
  <div class="user-box">
    <div style="margin-top: 20px; margin-left: 20px">
      <el-button icon="el-icon-plus" type="primary" @click="createUser">添加</el-button>
      <el-button icon="el-icon-delete" type="danger" @click="deleteBluk">批量删除</el-button>
    </div>
    <div style="padding: 15px; margin-top: 15px">
      <el-table :data="userInfoList" stripe style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="用户工号" prop="userNumber" width="150" />
        <el-table-column label="用户密码" prop="password" width="150" />
        <el-table-column label="用户类型" prop="userType">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.userType === '2'" type="danger">超级管理员</el-tag>
            <el-tag v-if="scope.row.userType === '1'" type="primary">管理员</el-tag>
            <el-tag v-if="scope.row.userType === '0'" type="success">普通用户</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="180" />
        <el-table-column label="最近修改时间" prop="updateTime" width="180" />
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="text" @click="updateUser(scope.row)">修改用户</el-button>
            <el-popconfirm style="margin-left: 15px" title="确定要删除该用户吗？" @confirm="deleteUser(scope.row.userId)">
              <el-button slot="reference" style="color: #f56c6c" type="text">删除用户</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :current-page="page"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="size"
        :page-sizes="[10, 15, 20, 30]"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    <!-- 添加/修改用户弹出层 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="userFormData">
        <el-form-item label="员工工号">
          <el-input
            v-model="userFormData.userNumber"
            placeholder="推荐使用年+月+日+第几个入职员工格式，如：231213001(2023年12月13日第一位入职员工)"
            style="width: 600px"
          />
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input v-model="userFormData.password" placeholder="请输入密码" show-password style="width: 600px" />
        </el-form-item>
        <el-form-item label="员工类型">
          <el-select v-model="userFormData.userType" placeholder="请选择" style="width: 180px">
            <el-option v-for="item in userTypeList1" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-select
            v-if="userFormData.userType === '0'"
            v-model="userFormData.userPermission"
            multiple
            placeholder="请选择"
            style="margin-left: 15px; width: 400px"
          >
            <el-option v-for="item in userTypeList2" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogTitle === '修改用户'" label="创建时间">
          <el-input v-model="userFormData.createTime" :disabled="true" style="width: 200px" />
        </el-form-item>
        <el-form-item v-if="dialogTitle === '修改用户'" label="修改时间">
          <el-input v-model="userFormData.updateTime" :disabled="true" style="width: 200px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUserCommit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import http_user from '@/api/user/user.js'
  export default {
    name: 'Permission',
    data() {
      return {
        page: 1,
        size: 10,
        total: 0,
        userInfoList: [],
        userFormData: {
          userId: '',
          userNumber: '',
          password: '',
          userType: '',
          userPermission: [],
          createTime: '',
          updateTime: '',
        },
        dialogFormVisible: false,
        dialogTitle: '',
        userTypeList1: [
          {
            value: '1',
            label: '管理员',
          },
          {
            value: '0',
            label: '普通员工',
          },
        ],
        userTypeList2: [
          {
            value: '3',
            label: '头像审核',
          },
          {
            value: '4',
            label: '职位审核',
          },
          {
            value: '5',
            label: '公司审核',
          },
          {
            value: '6',
            label: '消息回复',
          },
        ],
        selectUserId: [],
      }
    },
    mounted() {
      this.getAllUserInfo()
    },
    methods: {
      // 分页获得所有用户信息
      async getAllUserInfo() {
        const result = await http_user.getUserInfo(this.page, this.size)
        if (result.code === 200) {
          this.total = result.data.total
          this.userInfoList = result.data.records
        }
      },
      // 修改用户
      updateUser(data) {
        this.dialogTitle = '修改用户'
        Object.assign(this.userFormData, data)
        this.userFormData.userPermission = JSON.parse(this.userFormData.userPermission)
        this.dialogFormVisible = true
      },
      // 删除用户
      async deleteUser(userId) {
        if (JSON.parse(localStorage.getItem('userInfo')).userId === userId) {
          this.$message({
            message: '不能删除自己',
            type: 'warning',
          })
          return
        }
        const result = await http_user.deleteUser(userId)
        if (result.code === 200) {
          this.getAllUserInfo()
          this.$message({
            message: '删除用户成功',
            type: 'success',
          })
        }
      },
      // size该百年
      handleSizeChange(val) {
        this.size = val
        this.getAllUserInfo()
      },
      // page改变
      handleCurrentChange(val) {
        this.page = val
        this.getAllUserInfo()
      },
      // 创建用户
      createUser() {
        this.userFormData = {
          userId: '',
          userNumber: '',
          password: '',
          userType: '',
          userPermission: [],
          createTime: '',
          updateTime: '',
        }
        this.dialogTitle = '创建用户'
        this.dialogFormVisible = true
      },
      // 确认修改用户
      async updateUserCommit() {
        console.log(this.userFormData)
        if (this.userFormData.userNumber === '' || this.userFormData.password === '' || this.userFormData.userType === '') {
          this.$message({
            message: '请把数据填写完整',
            type: 'warning',
          })
        }
        if (this.userFormData.userType === '0' && this.userFormData.userPermission.length === 0) {
          this.$message({
            message: '请选择普通职员的工作范围',
            type: 'warning',
          })
        }
        if (this.dialogTitle === '创建用户') {
          const result_check = await http_user.checkNumber(this.userFormData.userNumber)
          if (result_check.code !== 200) {
            this.$message.error(result.msg)
            return
          }
        }
        if (this.userFormData.userType === '0') {
          this.userFormData.userPermission = JSON.stringify(this.userFormData.userPermission)
        } else {
          this.userFormData.userPermission = ''
        }

        if (this.dialogTitle === '修改用户') {
          const result_create = await http_user.updateUser(this.userFormData)
          if (result_create.code === 200) {
            this.dialogFormVisible = false
            this.getAllUserInfo()
            this.$message({
              message: '修改用户成功',
              type: 'success',
            })
          }
        } else if (this.dialogTitle === '创建用户') {
          const result_create = await http_user.createUser(this.userFormData)
          if (result_create.code === 200) {
            this.dialogFormVisible = false
            this.getAllUserInfo()
            this.$message({
              message: '创建用户成功',
              type: 'success',
            })
          }
        }
      },
      // 多选
      handleSelectionChange(val) {
        this.selectUserId = []
        for (let i = 0; i < val.length; i++) {
          this.selectUserId.push(val[i].userId)
        }
      },
      // 批量删除
      deleteBluk() {
        if (this.selectUserId.length === 0) {
          this.$message.error('请选择数据后在删除')
        }
        this.$confirm('确定要删除选中的用户吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const result = await http_user.deleteBluk(this.selectUserId)
            if (result.code === 200) {
              this.getAllUserInfo()
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
            }
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .user-box {
  }
</style>
